package com.zwps.biz.controller.listener.rabbitmq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;

import com.zwps.biz.api.model.dto.mointordentify.RecordVideoDTO;

import com.zwps.biz.domain.service.MonitorDeviceRecordVideoService;
import com.zwps.common.core.context.RabbitmqDefine;
import com.zwps.common.core.enums.MessageTypeEnum;
import com.zwps.common.core.service.LogService;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.IOException;


import static org.springframework.amqp.core.ExchangeTypes.TOPIC;

@Component
@ConditionalOnProperty("spring.rabbitmq.enable")
@AllArgsConstructor
public class MonitorRecordMQListener  {

    @Autowired
    private MonitorDeviceRecordVideoService  service;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private LogService logService;



    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = RabbitmqDefine.LHK_MONITOR_RECORD_MESSAGE_QUEUE, durable = "false"), // 配置队列
            exchange = @Exchange(value = RabbitmqDefine.LHK_MONITOR_ALARM_EXCHANGE_NAME, type = TOPIC), // 配置交换机，交换机类型
            key = { RabbitmqDefine.LHK_MONITOR_RECORD_MESSAGE_ROUTE } // 配置路由
    ), ackMode = "MANUAL")
    public void messageLogListener(Channel channel, Message message) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        logService.consumeRecord((body,logMessage)->{
        RecordVideoDTO recordVodeoDTO = objectMapper.readValue(body, RecordVideoDTO.class);
        service.addVideos(recordVodeoDTO);
        },message, MessageTypeEnum.RECOGNIZE_LISTEN_ALARM_VIDEO_SAMPLE_SYNC);

    }
}
